package com.ant.myagile

import com.ant.myagile.backlog.Issue
import com.ant.myagile.backlog.Sprint
import com.ant.myagile.backlog.UserStory;
import com.ant.myagile.dashboard.Status

class IssueService {

	def findIssueBySprintId(sprintID) {
		Sprint sprint = Sprint.findById(sprintID)
		return sprint.issues
	}
	
	//Author: Khang
	def findBugBySprintId(sprintID) {
		def bugs = Issue.findAll() {sprint == Sprint.get(sprintID) && type=="Bug"}
		return bugs
	}
	
	def SearchIssue(Hashtable<String, String> ht) {
		def sql="from Issue as i where 1=1"
		Enumeration keys = ht.keys();
		while( keys.hasMoreElements() ) {
		  Object key = keys.nextElement();
		  Object value = ht.get(key);
		  sql+=" and "+key+"='"+value+"'"
		}
		return Issue.findAll(sql)
	}

	def addNewIssue(sprintID, userStoryID) {
		Sprint sprint = Sprint.findById(sprintID)
		UserStory userStory = UserStory.findById(userStoryID)
		Team team = Team.get(sprint.team.id);		
		Status status = Status.findWhere(sprint : sprint, team : team, start : true)
		Issue issue = new Issue();
		issue.type = "UserStory"
		issue.subject = userStory.name
		issue.description = userStory.description
		issue.status = status
		issue.priority = 10
		issue.estimate = "D5T3"
		issue.remain = "D2T1"
		issue.sprint = sprint
		issue.userStory = userStory
		
		Issue result = issue.save(failOnError : true)
		
		return result;
	}
}
